package com.web.service.chapter;

import com.web.bean.chapter.Chapter;
import com.web.dao.chapter.ChapterDaoImpl;
import com.web.utils.DBUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @autor Chen
 * @create 2021--25-14:14
 * @desc
 */
public class ChapterServiceImpl implements ChapterService {

    /**
     *
     * @param chapter
     * @throws Exception
     */
    @Override
    public void addChapter(Chapter chapter) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();

        ChapterDaoImpl chapterDao = new ChapterDaoImpl(dbUtil.getConnection());
        chapterDao.addChapter(chapter);

        dbUtil.close();


    }

    /**
     *
     * @param chapter
     * @throws Exception
     */
    @Override
    public void updateChapter(Chapter chapter) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();

        ChapterDaoImpl chapterDao = new ChapterDaoImpl(dbUtil.getConnection());
        chapterDao.updateChapter(chapter);

        dbUtil.close();


    }

    /**
     *
     * @param id
     * @throws Exception
     */
    @Override
    public void deleteChapter(String id) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();

        ChapterDaoImpl chapterDao = new ChapterDaoImpl(dbUtil.getConnection());
        chapterDao.deleteChapterById(id);

        dbUtil.close();

    }

    /**
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public List<String> selectChapterById(String id) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        BufferedReader reader = null;
        String tempString = null;
        List<String> content = new ArrayList<>();
        int line = 1;
        ChapterDaoImpl chapterDao = new ChapterDaoImpl(dbUtil.getConnection());
        Chapter chapter = chapterDao.selectChapterById(id);
        String csrc = chapter.getCsrc();
        File file = new File(csrc);
        reader = new BufferedReader(new FileReader(file));
        while ((tempString = reader.readLine())!=null){
            content.add(tempString);
        }
        dbUtil.close();
        if (reader != null) {
            reader.close();
        }
        return content;
    }

    /**
     *
     * @param nid
     * @return
     * @throws Exception
     */
    @Override
    public List<Chapter> selectChapters(String nid) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        BufferedReader reader = null;
        String tempString = null;
        List<String> content = new ArrayList<>();
        ChapterDaoImpl chapterDao = new ChapterDaoImpl(dbUtil.getConnection());
        List<Chapter> chapters = chapterDao.selectChapters(nid);
        chapters = insertSort(chapters);
        dbUtil.close();
        return chapters;
    }


    /**
     * @param chapters
     * @return
     */
    private List<Chapter> insertSort(List<Chapter> chapters) {
        List<Chapter> chapterList = new ArrayList<>();
        for (int i = 0; i < chapters.size(); i++) {
            String[] s1 = chapters.get(i).getCname().split(" ");
            String num1 = s1[0].replaceAll("第","");
            num1 = num1.replaceAll("章","");
            boolean flag = false;
            if (chapterList.size() == 0) {
                chapterList.add(chapters.get(i));
            } else {
                for (int j = 0; j < chapterList.size(); j++) {
                    String[] s2 = chapterList.get(j).getCname().split(" ");
                    String num2 = s2[0].replaceAll("第","");
                    num2 = num2.replaceAll("章","");
                    if (Integer.parseInt(num1)<Integer.parseInt(num2)) {
                        chapterList.add(j, chapters.get(i));
                        flag = true;
                        break;
                    }
                }
                if (flag == false) {
                    chapterList.add(chapters.get(i));
                }
            }
        }
        return chapterList;
    }


}
